﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CsplCalculatorLib.Interface;

namespace CsplCalculatorLib
{
    public class BasicCalculator : IBasicCalculator
    {

        public int Add(int a, int b)
        {
            return a + b;
        }

        public int Mul(int a, int b)
        {
            return a * b;
        }

        public int Div(int a, int b)
        {
            int temp = 0;
            try
            {
                temp =  a / b;
            }
            catch (Exception e)
            {
                
                throw e;
            }
            return temp;
        }

        public int Sub(int a, int b)
        {
            return a - b;
        }


        public int AddList(IEnumerable<int> listOfNumber)
        {

            int temp = 0;

            foreach (int item in listOfNumber)
            {
                temp += item;
            }

            return temp;
        }


        public int ComplexListOperation(IEnumerable<int> listOfNumber, CalOperations caop)
        {
            int temp;

            if (caop == CalOperations.Mul || caop == CalOperations.Div)
            {

                temp = 1;
            }
            else {
                temp = listOfNumber.First();
            }

            foreach (int number in listOfNumber)
            {
                switch (caop)
                {
                    case CalOperations.Add:
                        temp = this.Add(temp,number);
                        break;
                    case CalOperations.Mul:
                        temp = this.Mul(temp,number);
                        break;
                    case CalOperations.Div:
                        temp = this.Div(temp,number);
                        break;
                    case CalOperations.Sub:
                        temp = this.Sub(temp,number);
                        break;                    
                }
            }

            return temp;
        }
    }
}
